<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="自制样品名称：" prop="sampleName">
              <el-input clearable maxlength="100" placeholder="请输入自制样品名称" v-model="queryParams.sampleName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="唯一性编号：" prop="sampleCode">
              <el-input clearable maxlength="100" placeholder="请输入唯一性编号" v-model="queryParams.sampleCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="配置人：" prop="selfAdminId" label-width="65">
              <el-select v-model="queryParams.selfAdminId" clearable filterable>
                <el-option v-for="item in proxy.addListNoLimit(userList, 'userId', 'nickName')" :key="item.userId"
                  :value="item.userId" :label="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="配置日期：" label-width="80">
              <el-date-picker value-format="YYYY-MM-DD" type="daterange" v-model="dateRange" range-separator="-"
                start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目：" prop="bucketId" label-width="80">
              <el-select v-model="queryParams.bucketId" clearable filterable remote reserve-keyword remote-show-suffix
                :remote-method="remoteMethod" :loading="loading" style="width: 100%">
                <el-option v-for="item in jcxmList" :key="item.bucketId" :value="item.bucketId" :label="item.jcxmName">
                  <span v-html="transformation(item.jcxmName)"></span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="检测项目类型：" prop="isSingle">
              <el-select placeholder="请选择" clearable v-model="queryParams.isSingle" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_jcxm_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3" style="padding-left: 30px;">
            <el-row>
              <el-radio label="0" v-model="queryParams.isLike">模糊搜索</el-radio>
              <el-radio label="1" v-model="queryParams.isLike">精准搜索</el-radio>
            </el-row>
          </el-col>
          <el-col :span="2">
            <el-row>
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">自制样品列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <!-- <el-row style="padding-bottom: 5px; margin-left: 20px">
          <el-button type="success" link icon="DocumentDelete" @click="handleExport">导出台账</el-button>
        </el-row> -->
        <el-table ref="mainTableRef" :data="ledgerTable">
          <el-table-column label="唯一性编号" prop="sampleCode" align="center" />
          <el-table-column label="自制样品名称" prop="sampleName" align="center" />
          <el-table-column label="种类" prop="sampleType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_self_sample_type" :value="scope.row.sampleType" />
            </template>
          </el-table-column>
          <el-table-column label="溶剂" prop="rj" align="center">
          </el-table-column>
          <el-table-column label="定容体积" prop="drtj" align="center">
            <template #default="scope">
              <span v-html="scope.row.drtj + transformation(scope.row.drtjUnitName)"></span>
            </template>
          </el-table-column>
          <el-table-column label="配置日期" prop="createDate" align="center" />
          <el-table-column label="有效日期" prop="yxDate" align="center" />
          <el-table-column label="保存条件" prop="saveCondition" align="center" />
          <el-table-column label="保存位置" prop="saveLocation" align="center" />
          <el-table-column label="配置人" prop="selfAdminId" align="center">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.selfAdminId,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
          <el-table-column label="检测项目数" prop="jcxmCount" align="center">
            <template #default="scope">
              <el-button link @click="jcxmCountClick(scope.row.sampleId)">{{
                scope.row.jcxmName ? scope.row.jcxmName : scope.row.jcxmCount
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="余量" prop="remainDrtj" align="center" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="success" @click="xqZzyp(scope.row.sampleId)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击详情弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="open" :title="xqTitle" @close="closedXqDialog">
      <el-form label-width="100" ref="xqRef" v-model="form">
        <el-row>
          <el-col :span="8">
            <el-form-item label="自制样品名称：" prop="sampleName">
              {{ form.sampleName || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="唯一性编号：" prop="sampleCode">
              {{ form.sampleCode }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="种类：" prop="sampleType">
              <dict-tag :options="sys_self_sample_type" :value="form.sampleType" v-if="form.sampleType" />
              <span v-else>/</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库日期：" prop="createDate">
              {{ form.createDate || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="有效期：" prop="yxDate">
              {{ form.yxDate || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="溶剂：" prop="rj">
              {{ form.rj || "/" }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="定容体积：" prop="drtj">
              <span v-html="form.drtj + transformation(form.drtjUnitName)"></span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="天平编号：" prop="tpCode">
              {{
                proxy.getItemByArray(
                  tpCodeList,
                  "instrumentId",
                  form.tpCode,
                  "number"
                ) || "/"
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="天平型号：" prop="tpTh">
              {{ form.tpXh || "/" }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <p class="pinformation">选择参与物质列表</p>
          <p class="totalStyle">
            共 {{ form.selfSampleBzwzList.length }} 条数据
          </p>
        </el-row>
        <el-table :data="form.selfSampleBzwzList" style="margin-bottom: 10px">
          <el-table-column label="标准物质名称" prop="name" align="center" />
          <el-table-column label="唯一编号" prop="outCode" align="center" />
          <el-table-column label="纯度" prop="con" align="center" />
          <el-table-column label="生产日期" prop="outDate" align="center" />
          <el-table-column label="取样量" prop="qyl" align="center" />
          <el-table-column label="取样量单位" prop="qylUnitName" align="center" />
        </el-table>
        <el-row>
          <p class="pinformation">检测项目列表</p>
          <p class="totalStyle">
            共 {{ form.selfSampleJcxmList.length }} 条数据
          </p>
        </el-row>
        <el-table :data="form.selfSampleJcxmList">
          <el-table-column label="检测项目" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.jcxmName)"></span>
            </template>
          </el-table-column>
          <el-table-column label="浓度值" prop="con" align="center" />
          <el-table-column label="浓度值单位" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.conUnitName)
                "></span>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ jcxmTable.length }} 条数据</p>
      </el-row>
      <el-table :data="jcxmTable">
        <el-table-column label="检测项目" prop="bucketId" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SelfSampleLedger">
import {
  newListLedger,
  sampleList,
  zzypViewForm,
  getTpCodeList,
  getBzwzList,
  getJcxmList,
} from "@/api/resource/selfSampleLedger";

import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();
const { sys_self_sample_type, sys_bzwz_jcxm_type } = proxy.useDict(
  "sys_self_sample_type",
  "sys_bzwz_jcxm_type"
);

const ledgerTable = ref([]);
const jcxmTable = ref([]);
const dateRange = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const total = ref(0);
const open = ref(false);
const xqTitle = ref("查看自制样品详情");
const data = reactive({
  form: { selfSampleBzwzList: [], selfSampleJcxmList: [] },
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    isLike: "0",
    selfAdminId: -1,
    isSingle: -1
  },
  rules: {
    // packageName: [{ required: true, message: "包名不能为空", trigger: "blur" }],
    // jclbId: [{ required: true, message: "检测类别不能为空", trigger: "blur" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

const jcxmList = ref([]);
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function getList() {
  newListLedger(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      ledgerTable.value = res.rows;
      total.value = res.total;
    }
  );
}
getList();

const unitList = ref([]);
getUnitList({ unitGroups: [0, 5] }).then((res) => {
  unitList.value = res.data;
});

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  queryParams.value.isLike = "0"
  handleQuery();
}

function jcxmCountClick(row) {
  sampleList({ sampleId: row }).then((res) => {
    jcxmTable.value = res.data;
    jcxmOpen.value = true;
  });
}

function closedDialog() {
  jcxmTable.value = [];
}

function xqZzyp(sampleId) {
  zzypViewForm(sampleId).then((res) => {
    form.value = res.data;
    open.value = true;
  });
}

function closedXqDialog() {
  form.value = {
    selfSampleBzwzList: [],
    selfSampleJcxmList: [],
  };
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const tpCodeList = ref([]);
getTpCodeList({ typeId: 2 }).then((res) => {
  tpCodeList.value = res.data;
});

const bzwzList = ref([]);
getBzwzList().then((res) => {
  bzwzList.value = res.data;
});

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/resource/sample/export",
    {
      ...queryParams.value,
    },
    `自制样品入库登记表_${new Date().getTime()}.xlsx`
  );
}

const loading = ref(false);

function remoteMethod(val) {
  if (val) {
    loading.value = true;
    setTimeout(() => {
      getJcxmList({ jcxmName: val }).then((res) => {
        loading.value = false;
        jcxmList.value = res.rows;
      });
    }, 200);
  } else {
    jcxmList.value = [];
  }
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
